package com.huiquan.icd.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.BaseService;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.GetListUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.icd.dao.IcdDiseaseImageDao;
import com.huiquan.icd.domain.IcdDiseaseImage;

@Service
public class IcdDiseaseImageService extends BaseService {
	/** 疾病图片的虚拟路径 */
	public static final String ORIGINAL_IMAGE_PATH = "/diseaseImages/original/";
	public static final String THUMBNAIL_IMAGE_PATH = "/diseaseImages/thumbnail/";
	@Autowired
	private IcdDiseaseImageDao icdDiseaseImageDao;

	/**
	 * 获取疾病图片列表
	 * 
	 * @param start
	 * @param keyword
	 * @return
	 */
	public ModelAndView getList(Integer start, String keyword,String countPerPageStr) {
		Map<String, Object> param = new HashMap<>();
		if (keyword != null && !keyword.isEmpty()) {
			param.put("keyword", "%" + keyword + "%");
		}
		int totalSize = icdDiseaseImageDao.retrieveSize(param);

		Map<String, Integer> pageNo = new HashMap<>();
		if (countPerPageStr != null && !countPerPageStr.isEmpty()) {
			int countPerPage = Integer.parseInt(countPerPageStr);
			pageNo = GetListUtil.getPageNoParam(totalSize, start+"", countPerPage);
			param.put("countPerPage", countPerPage);
		} else {
			pageNo = GetListUtil.getPageNoParam(totalSize, start+"");
		}
		int endPage = pageNo.get("endPage");
		start = pageNo.get("start");
		int startIndex = pageNo.get("startIndex");

		List<IcdDiseaseImage> list = new ArrayList<>();

		if (startIndex < totalSize) {
			param.put("startIndex", startIndex);
			list = icdDiseaseImageDao.retrieveList(param);
		}

		Map<String, String> showSearch = new LinkedHashMap<>();
		showSearch.put("keyword", "疾病");
		
		Map<String, Object> map = GetListUtil.getReturnMap2(totalSize, start, startIndex, endPage, list, null);
		map.put("showSearch", showSearch);
		return new ModelAndView("icd/index", map);
	}

	/** 上传图片保存路径 */
	@Value("${disease.image.original.path}")
	private String uploadOriginalImagePath;
	@Value("${disease.image.thumbnail.path}")
	private String uploadThumbnailImagePath;
	/**
	 * 插入一个检验大类
	 * 
	 * @param params
	 * @param user
	 * @param image
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	public ReturnData insert(Map<String, String> params, User user, MultipartFile originalImage,
			MultipartFile thumbnailImage) throws IllegalStateException, IOException {
		String originalName = originalImage.getOriginalFilename();
		String thumbnailName = thumbnailImage.getOriginalFilename();
		int count1 = icdDiseaseImageDao.retrieveSizeByName(originalName, null);
		int count2 = icdDiseaseImageDao.retrieveSizeByName(thumbnailName, null);
		if ((count1 + count2) > 0 || StringUtils.equalsIgnoreCase(originalName, thumbnailName)) {
			return ReturnUtil.fail(ReturnCode.DISEASE_IMAGE_NAME_EXIST);
		}

		// 上传文件并获得文件URL
		String originalImageURL = uploadImage(originalImage,uploadOriginalImagePath, originalName,ORIGINAL_IMAGE_PATH);
		// 上传文件并获得文件URL
		String thumbnailImageURL = uploadImage(thumbnailImage,uploadThumbnailImagePath, thumbnailName,THUMBNAIL_IMAGE_PATH);
		params.put("thumbnailImageURL", thumbnailImageURL);
		params.put("originalImageURL", originalImageURL);
		params.put("originalPicture", originalName);
		params.put("thumbnailPicture", thumbnailName);
		params.put("uid", user.getUserId() + "");
		// 插入到mysql
		icdDiseaseImageDao.addImage(params);

		return ReturnUtil.success();
	}

	/**
	 * 批量导入图片
	 * 
	 * @param user
	 * @param zipFile
	 * @return
	 * @throws IOException
	 */
//	public ReturnData insertBatchs(User user, MultipartFile excel, MultipartFile zipFile) throws IOException {
//		// 解析excel
//		InputStream inputStream = null;
//		Workbook workbook = null;
//		// InputStreamReader read = new InputStreamReader(in, "UTF-8"); //
//		// 考虑到文本编码
//		// BufferedReader bf = new BufferedReader(read);
//		// String line = null;
//		// String s[] = null;
//		// Map<String, String> params = null;
//		// while ((line = bf.readLine()) != null) {
//		// s = line.split("\t");
//		// if (s.length == 5) {
//		// params = new HashMap<>();
//		// params.put("name", s[0]);
//		// params.put("id", s[2]);
//		// params.put("rank", s[3]);
//		// params.put("disable", s[4]);
//		// createClass(params, s[1], InspectionConstant.FICTITIOUS_IMAGE_PATH +
//		// s[1], user);
//		// }
//		//
//		// }
//		// read.close();
//		try {
//			// 获取文件流，解析文件
//			inputStream = excel.getInputStream();
//
//			workbook = new XSSFWorkbook(inputStream);
//			Sheet sheet = workbook.getSheetAt(0);
//			// 删除文件头
//			ExcelUtil.deleteTitleRow(sheet);
//			// 所有图片的名字 校验是否重复
//			List<String> allImageName = new ArrayList<>();
//			// 错误的名字
//			List<String> errName = new ArrayList<>();
//			List<IcdDiseaseImage> list = new ArrayList<>();
//			for (Row row : sheet) {
//				row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
//				String name = row.getCell(0).getStringCellValue();
//				row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
//				String originalName = row.getCell(1).getStringCellValue();
//				row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
//				String thumbnailName = row.getCell(2).getStringCellValue();
//				row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
//				String rank = row.getCell(3).getStringCellValue();
//				// 校验图片名称
//				if (allImageName.contains(originalName) || allImageName.contains(thumbnailName)) {
//					// 如果图片名字有重复则提示
//					errName.add(name);
//				} else {
//					allImageName.add(originalName);
//					allImageName.add(thumbnailName);
//				}
//				IcdDiseaseImage di = new IcdDiseaseImage();
//				di.setDisease(name);
//				di.setRank(Long.parseLong(rank));
//				di.setOriginalPicture(originalName);
//				di.setThumbnailPicture(thumbnailName);
//				di.setThumbnailURL("");
//				di.setOriginalURL("");
//				di.setModifierId(user.getUserId() + "");
//				list.add(di);
//			}
//			if (errName != null && errName.size() > 0) {
//				ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
//				StringBuffer err = new StringBuffer();
//				for (String name : errName) {
//					err.append(name).append(",");
//				}
//				rc.setDesc("以下疾病的名字重复:" + err.toString().substring(0, err.length() - 1));
//				return ReturnUtil.fail(rc);
//			}
//			icdDiseaseImageDao.deleteAll();
//			icdDiseaseImageDao.addBatch(list);
//
//		} catch (Exception e) {
//			throw e;
//		} finally {
//			workbook.close();
//			inputStream.close();
//		}
//
//		// 图片zip解析
//		ZipFile zf = null;
//		try {
//			File file = File.createTempFile("tempZip", ".zip");
//			zipFile.transferTo(file);
//			zf = new ZipFile(file);
//			ZipEntry ze = null;
//			String fileName = null;
//			InputStream in = null;
//			Enumeration<ZipEntry> em = zf.getEntries();
//			// 删除目录下所有文件
//			delAllFile(uploadImagePath);
//			while (em.hasMoreElements()) {
//				ze = em.nextElement();
//				// 如果当前条目是目录，报错
//				if (ze.isDirectory()) {
//					zf.close();
//					return ReturnUtil.fail(ReturnCode.IMPORT_ZIP_DIRECTORY_ERROR);
//				}
//				fileName = ze.getName();
//				if (fileName.endsWith(".png") || fileName.endsWith(".PNG")) {
//					// 用图片名字查询记录
//					IcdDiseaseImage di = icdDiseaseImageDao.retrieveByName(fileName);
//					// 如果图片没有对应图片名字则提示
//					if (di == null) {
//						ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
//						rc.setDesc("以下文件的名字没有对应的记录:" + fileName);
//						return ReturnUtil.fail(rc);
//					}
//					in = zf.getInputStream(ze);
//					// 上传图片
//					decompressionPng(in, fileName, uploadImagePath);
//					// 更新图片url
//					if (StringUtils.equalsIgnoreCase(di.getOriginalPicture(), fileName)) {
//						di.setOriginalURL(FICTITIOUS_IMAGE_PATH + fileName);
//					} else if (StringUtils.equalsIgnoreCase(di.getThumbnailPicture(), fileName)) {
//						di.setThumbnailURL(FICTITIOUS_IMAGE_PATH + fileName);
//					}
//					di.setModifierId(user.getUserId() + "");
//					icdDiseaseImageDao.update(di);
//				}
//			}
//		} catch (Exception e) {
//			throw e;
//		} finally {
//			if (zf != null) {
//				zf.close();
//			}
//		}
//		return ReturnUtil.success();
//	}

	/**
	 * 批量导入图片
	 * 
	 * @param user
	 * @param zipFile
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("resource")
	public ReturnData insertBatch(User user, MultipartFile zipFile) throws IOException {

		// 图片zip解析
		// 需要先解析text文件，拿到所有文件
		ZipFile zf = null;
		// 文件名字对应流缩图
		Map<String, InputStream> thumbnailMap = new HashMap<>();
		Map<String, InputStream> originalMap = new HashMap<>();

			File file = File.createTempFile("tempZip", ".zip");
			zipFile.transferTo(file);
			zf = new ZipFile(file);
			
			ZipEntry ze = null;
			String fileName = null;
			InputStream in = null;
			Enumeration<ZipEntry> em = zf.getEntries();
			while (em.hasMoreElements()) {
				ze = em.nextElement();
				// 如果当前条目是目录，报错
				if (ze.isDirectory()) {
					continue;
				}
				fileName = ze.getName();
				if ((fileName.endsWith(".png") || fileName.endsWith(".PNG")) && fileName.startsWith("thumbnail")) {
					// 图片名和流
					String[] s = fileName.split("/");
					in = zf.getInputStream(ze);
					thumbnailMap.put(s[1], in);
				} else if (fileName.endsWith(".txt") || fileName.endsWith(".TXT")) {
					// 文件
					in = zf.getInputStream(ze);
					thumbnailMap.put("txt", in);
				}else if((fileName.endsWith(".png") || fileName.endsWith(".PNG")) && fileName.startsWith("original")){
					// 图片名和流
					String[] s = fileName.split("/");
					in = zf.getInputStream(ze);
					originalMap.put(s[1], in);
				}
			}

		InputStream textIn = null;
		//记录中的图片名
		List<String> allImageName = new ArrayList<>();
		//提示只有图片名没有图片的数据
		List<String > errImageName = new ArrayList<>();
		try {
			// 解析txt文件
			textIn = thumbnailMap.get("txt");
			// 所有图片的名字 校验是否重复
			List<String> errName = new ArrayList<>();
			List<IcdDiseaseImage> list = new ArrayList<>();
			if (textIn != null) {
				InputStreamReader read = new InputStreamReader(textIn, "UTF-8"); // 考虑到文本编码
				BufferedReader bf = new BufferedReader(read);
				String line = null;
				String s[] = null;
				while ((line = bf.readLine()) != null) {
					s = line.split("\t");
					if (s.length == 4) {
						String name = s[0];
						String originalName = s[1];
						String thumbnailName = s[2];
						String rank = s[3];
						// 校验图片名称
						if (allImageName.contains(originalName) || allImageName.contains(thumbnailName)) {
							// 如果图片名字有重复则提示
							errName.add(name);
						} else {
							allImageName.add(originalName);
							allImageName.add(thumbnailName);
						}
						IcdDiseaseImage di = new IcdDiseaseImage();
						di.setDisease(name);
						di.setRank(Long.parseLong(rank));
						di.setOriginalPicture(originalName);
						di.setThumbnailPicture(thumbnailName);
						di.setThumbnailURL("");
						di.setOriginalURL("");
						di.setModifierId(user.getUserId() + "");
						list.add(di);
					}
				}
			}
			if (errName != null && errName.size() > 0) {
				ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
				StringBuffer err = new StringBuffer();
				for (String name : errName) {
					err.append(name).append(",");
				}
				rc.setDesc("以下疾病的名字的图片重复:" + err.toString().substring(0, err.length() - 1));
				return ReturnUtil.fail(rc);
			}
			if(list != null && list.size()>0){
				icdDiseaseImageDao.deleteAll();
				icdDiseaseImageDao.addBatch(list);
			}
		} catch (Exception e) {
			// TODO: handle exception
			throw e;
		} finally {
			if (textIn != null) {
				textIn.close();
			}
		}
		
		//循环处理图片
		InputStream imageIn = null;
//		// 删除目录下所有文件
//		delAllFile(uploadOriginalImagePath);
//		delAllFile(uploadThumbnailImagePath);
		try{
			for(String imageName:allImageName ){
				if(originalMap.get(imageName)!= null){
					imageIn  = originalMap.get(imageName);
					if(imageIn != null){
						// 用图片名字查询记录
						IcdDiseaseImage di = icdDiseaseImageDao.retrieveByName(imageName);
						// 如果图片没有对应图片名字则不处理
						if (di != null) {
							// 上传图片
							decompressionPng(imageIn, imageName, uploadOriginalImagePath);
							// 更新图片url
							if (StringUtils.equalsIgnoreCase(di.getOriginalPicture(), imageName)) {
								di.setOriginalURL(ORIGINAL_IMAGE_PATH + imageName);
							} else if (StringUtils.equalsIgnoreCase(di.getThumbnailPicture(), imageName)) {
								di.setThumbnailURL(THUMBNAIL_IMAGE_PATH + imageName);
							}
							di.setModifierId(user.getUserId() + "");
							icdDiseaseImageDao.update(di);
						}
					}else{
						errImageName.add(imageName);
					}
				}else{
					imageIn  = thumbnailMap.get(imageName);
					if(imageIn != null){
						// 用图片名字查询记录
						IcdDiseaseImage di = icdDiseaseImageDao.retrieveByName(imageName);
						// 如果图片没有对应图片名字则不处理
						if (di != null) {
							// 上传图片
							decompressionPng(imageIn, imageName, uploadThumbnailImagePath);
							// 更新图片url
							if (StringUtils.equalsIgnoreCase(di.getOriginalPicture(), imageName)) {
								di.setOriginalURL(ORIGINAL_IMAGE_PATH + imageName);
							} else if (StringUtils.equalsIgnoreCase(di.getThumbnailPicture(), imageName)) {
								di.setThumbnailURL(THUMBNAIL_IMAGE_PATH + imageName);
							}
							di.setModifierId(user.getUserId() + "");
							icdDiseaseImageDao.update(di);
						}
					}else{
						errImageName.add(imageName);
					}
				}

			}
		} catch (Exception e) {
			// TODO: handle exception
			throw e;
		} finally {
			if (textIn != null) {
				textIn.close();
			}
			if(zf !=null){
				zf.close();
			}
		}
		if(errImageName != null && errImageName.size()>0){
			StringBuffer err = new StringBuffer();
			for (String name : errImageName) {
				err.append(name).append(",");
			}
			return ReturnUtil.success("导入成功、以下记录的图片名没有对应图片" + err.toString().substring(0, err.length() - 1));
		}
		return ReturnUtil.success("导入成功");
	}

	/**
	 * 更新疾病图片或缩图
	 * 
	 * @param id
	 * @param user
	 * @param image
	 * @return
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	public ReturnData updateImage(String id, String type, User user, MultipartFile image)
			throws IllegalStateException, IOException {
		IcdDiseaseImage tc = icdDiseaseImageDao.retrieveObjectById(Long.parseLong(id));
		if (StringUtils.equalsIgnoreCase(type, "1")) {
			String oldImageName = tc.getOriginalPicture();
			String originalPicture = image.getOriginalFilename();
			// 校验名称是否正确
			if (checkImageName(originalPicture, id) > 0) {
				return ReturnUtil.fail(ReturnCode.DISEASE_IMAGE_NAME_EXIST);
			} else {
				// 同一条记录如果2个名字一样也不行
				if (StringUtils.equalsIgnoreCase(originalPicture, tc.getThumbnailPicture())) {
					return ReturnUtil.fail(ReturnCode.DISEASE_IMAGE_NAME_EXIST);
				}
			}
			// 先把图片进行替换
			String path = uploadOriginalImagePath + originalPicture;
			File file = new File(path);
			// 更新或覆盖原图片
			image.transferTo(file);
			// 如果新图片名和原图片名不同，要更新图片名和URL
			if (!originalPicture.equals(oldImageName)) {
				// 如果名字不一样更新名字
				renameFile(uploadThumbnailImagePath, oldImageName, originalPicture);
				tc.setOriginalPicture(originalPicture);
				tc.setOriginalURL(tc.getOriginalURL().replace(oldImageName, originalPicture));
				tc.setModifierId(user.getUserId() + "");
				icdDiseaseImageDao.update(tc);
			}

		} else if (StringUtils.equalsIgnoreCase(type, "2")) {
			String thumbnailPicture = image.getOriginalFilename();
			// 校验名称是否正确
			if (checkImageName(thumbnailPicture, id) > 0) {
				return ReturnUtil.fail(ReturnCode.DISEASE_IMAGE_NAME_EXIST);
			} else {
				// 同一条记录如果2个名字一样也不行
				if (StringUtils.equalsIgnoreCase(thumbnailPicture, tc.getOriginalPicture())) {
					return ReturnUtil.fail(ReturnCode.DISEASE_IMAGE_NAME_EXIST);
				}
			}
			String oldImageName = tc.getThumbnailPicture();
			String path = uploadThumbnailImagePath + thumbnailPicture;
			File file = new File(path);
			// 更新或覆盖原图片
			image.transferTo(file);
			// 如果新图片名和原图片名不同，要更新图片名和URL
			if (!thumbnailPicture.equals(oldImageName)) {
				renameFile(uploadThumbnailImagePath, oldImageName, thumbnailPicture);
				tc.setThumbnailPicture(thumbnailPicture);
				tc.setThumbnailURL(tc.getThumbnailURL().replace(oldImageName, thumbnailPicture));
				tc.setModifierId(user.getUserId() + "");
				icdDiseaseImageDao.update(tc);
			}
		}
		return ReturnUtil.success();
	}

//	// 删除目录下所有文件
//	public static boolean delAllFile(String path) {
//		boolean flag = false;
//		File file = new File(path);
//		if (!file.exists()) {
//			return flag;
//		}
//		if (!file.isDirectory()) {
//			return flag;
//		}
//		String[] tempList = file.list();
//		File temp = null;
//		for (int i = 0; i < tempList.length; i++) {
//			if (path.endsWith(File.separator)) {
//				temp = new File(path + tempList[i]);
//			} else {
//				temp = new File(path + File.separator + tempList[i]);
//			}
//			if (temp.isFile()) {
//				temp.delete();
//			}
//		}
//		return flag;
//	}

	/**
	 * 上传图片到服务器
	 * 
	 * @param image
	 *            图片文件流
	 * @param imageName
	 *            图片名
	 * @return
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	private String uploadImage(MultipartFile image,String imagePath, String imageName,String paths) throws IllegalStateException, IOException {
		String path = imagePath + imageName;
		File file = new File(path);
		image.transferTo(file);
		return paths + imageName;
	}

	/**
	 * 删除一个TestingClass
	 * 
	 * @param id
	 */
	public void deleteClass(String id) {
		IcdDiseaseImage tc = new IcdDiseaseImage();
		tc.setId(Long.valueOf(id));
		icdDiseaseImageDao.delete(tc);
	}


	public ReturnData update(String id, String disease, String originalName, String thumbnailName, String rank,
			User user) {
		IcdDiseaseImage tc = icdDiseaseImageDao.retrieveObjectById(Long.parseLong(id));

		if (StringUtils.isNotEmpty(disease)) {
			tc.setDisease(disease);
		}
		if (StringUtils.isNotEmpty(originalName)) {
			if (checkImageName(originalName, id) > 0) {
				return ReturnUtil.fail(ReturnCode.DISEASE_IMAGE_NAME_EXIST);
			} else {
				// 同一条记录如果2个名字一样也不行
				if (StringUtils.equalsIgnoreCase(originalName, tc.getThumbnailPicture())) {
					return ReturnUtil.fail(ReturnCode.DISEASE_IMAGE_NAME_EXIST);
				}
			}
			// 更新服务器上的文件名
			renameFile(uploadOriginalImagePath, tc.getOriginalPicture(), originalName);
			String imageURL = tc.getOriginalURL();
			imageURL = imageURL.replace(tc.getOriginalPicture(), originalName);
			tc.setOriginalURL(imageURL);
			tc.setOriginalPicture(originalName);
		}
		if (StringUtils.isNotEmpty(thumbnailName)) {
			if (checkImageName(thumbnailName, id) > 0) {
				return ReturnUtil.fail(ReturnCode.DISEASE_IMAGE_NAME_EXIST);
			} else {
				// 同一条记录如果2个名字一样也不行
				if (StringUtils.equalsIgnoreCase(thumbnailName, tc.getOriginalPicture())) {
					return ReturnUtil.fail(ReturnCode.DISEASE_IMAGE_NAME_EXIST);
				}
			}
			// 更新服务器上的文件名
			renameFile(uploadThumbnailImagePath, tc.getThumbnailPicture(), thumbnailName);
			String imageURL = tc.getOriginalURL();
			imageURL = imageURL.replace(tc.getThumbnailPicture(), thumbnailName);
			tc.setThumbnailURL(imageURL);
			tc.setThumbnailPicture(thumbnailName);
		}
		if (StringUtils.isNotEmpty(rank)) {
			tc.setRank(Long.parseLong(rank));
		}
		tc.setModifierId(user.getUserId() + "");
		icdDiseaseImageDao.update(tc);

		return ReturnUtil.success();
	}

	private int checkImageName(String name, String id) {
		return icdDiseaseImageDao.retrieveSizeByName(name, id);
	}

	/**
	 * 服务器文件重命名
	 * 
	 * @param path
	 * @param oldname
	 * @param newname
	 */
	private void renameFile(String path, String oldname, String newname) {
		if (!oldname.equals(newname)) {// 新的文件名和以前文件名不同时,才有必要进行重命名
			File oldfile = new File(path + "/" + oldname);
			File newfile = new File(path + "/" + newname);
			if (!oldfile.exists()) {
				// 重命名文件不存在
				LOGGER.info("文件不存在");
				return;
			}
			if (newfile.exists())// 若在该目录下已经有一个文件和新文件名相同，则不允许重命名
				LOGGER.info(newname + "已经存在！");
			else {
				oldfile.renameTo(newfile);
			}
		} else {
			LOGGER.info("新文件名和旧文件名相同");
		}
	}

	/**
	 * 解压出PNG图片
	 * 
	 * @param in
	 * @param fileName
	 * @param decompressionPath
	 * @throws IOException
	 */
	private void decompressionPng(InputStream in, String fileName, String decompressionPath) throws IOException {
		byte[] buf = new byte[1024];
		int len = 0;
		OutputStream ot = null;
		try {
			ot = new FileOutputStream(decompressionPath + fileName);
			while ((len = in.read(buf)) != -1) {
				ot.write(buf, 0, len);
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if (ot != null) {
				ot.flush();
				ot.close();
			}
			if (in != null) {
				in.close();
			}

		}
	}

}
